// miniprogram/pages/question.js
const { $Message } = require('../../dist/base/index.js');
const db = wx.cloud.database();//获取云数据库引用
const dbuser = db.collection('user');//获取数据库 user 的引用
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    total:60,
    visible1: false,
    imgArr:[],
    S:['A','B','C','D','E','F'],
    userAnswers:[],
    rightAnswers:[],
    grade: [
      [60,52, 49, 44, 33, 26, 18, 17,0],
      [60,54, 52, 46, 37, 30, 22, 19,0],
      [60,54, 52, 48, 42, 34, 24, 21,0],
      [60,57, 54, 50, 47, 41, 31, 28,0],
      [60,57, 55, 52, 48, 34, 37, 28,0],
      [60,57, 56, 54, 50, 44, 38, 33,0],
      [60,58, 57, 55, 52, 47, 40, 37,0],
      [60,57, 56, 53, 49, 45, 41, 37,0],
      [60,57, 56, 53, 49, 44, 41, 36,0],
      [60,57, 55, 52, 49, 43, 41, 34,0],
      [60,57, 54, 51, 48, 43, 36, 34,0],
      [60,55, 53, 51, 48, 43, 36, 34,0],
      [60,55, 52, 50, 48, 43, 36, 34,0],
      [60,54, 52, 50, 46, 42, 35, 32,0],
      [60,53, 52, 50, 45, 40, 35, 30,0],
      [60,53, 52, 50, 45, 40, 33, 28,0],
      [60,53, 50, 46, 42, 37, 27, 21,0],
      [60,53, 50, 45, 42, 35, 25, 19,0],
      [60,52, 50, 43, 39, 33, 25, 19,0],
      [60,50, 49, 42, 39, 32, 25, 18,0],
      [60,50, 48, 42, 35, 27, 17, 13,0],
      [60,50, 47, 39, 35, 27, 17, 13,0],
      [60,47, 43, 37, 33, 25, 14, 12,0],
      [60,45, 40, 33, 29, 20, 14, 12,0],
      [60,44, 39, 31, 23, 15, 13, 10,0],
      [60,44, 38, 31, 21, 13, 12, 10,0],
      [60,43, 36, 25, 19, 13, 12, 10,0],
      [60,37, 31, 25, 18, 13, 12, 10,0],
      [60,36, 31, 25, 17, 13, 12, 9,0],
      [60,34, 29, 25, 16, 13, 12, 9,0]
    ],
    standard:[100,95,90,75,50,25,10,5,0],
    iq: [
      ["优秀", "高于95"],
      ["良好", "75~94"],
      ["中等", "25~74"],
      ["中下", "5~24"],
      ["低下", "低于5"],
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      imgArr:app.globalData.problems,
    })
  },
  onReady: function(){
    let arr = new Array(60).fill(0);
    this.setData({
      userAnswers:arr
    })
  },
// 分页
//下一题
next(){
    this.setData({
      current: this.data.current + 1,
  });
},

//上一题
pre(){
    this.setData({
      current: this.data.current - 1,
  });
},

//动作面板
handleOpen1 () {
  this.setData({
      visible1: true
  });
},

handleCancel1 () {
  this.setData({
      visible1: false
  });
},

jumpPage(e){
  // console.log(e.currentTarget.id)
  this.setData({
    current:parseInt(e.currentTarget.id)+1
  })
    this.handleCancel1()
},

// 答案选项
radioChange: function (e) {
  var userAnswers = this.data.userAnswers
  // console.log(this.data.userAnswers)
  userAnswers[this.data.current-1] = e.detail.value
  this.setData({
    userAnswers:userAnswers
  })
  // console.log(this.data.userAnswers)
},

// 提交绑定
submit:function(){
  for(var i=0; i<60; i++){
    this.data.rightAnswers[i] = app.globalData.problems[i].rightAnswer
  }

  //获取结束时间
  app.globalData.endTime = +new Date();
  //计算用时
  this.getUseTime()

  //计算等级分数
  this.getGrade();

   //记录数据
   this.record();

  //计算完成，跳转至结果展示页面
  wx.redirectTo({
    url: '../grade/grade',
  })
},

//计算等级分数
getGrade() {
  //先清空
  app.globalData.rightA = 0
  app.globalData.rightB = 0
  app.globalData.rightC = 0
  app.globalData.rightD = 0
  app.globalData.rightE = 0
  
  for(var j=0; j<this.data.total; j++){
    var group = app.globalData.problems[j].group
    switch(group){
      case "A":
        this.data.rightAnswers[j]==this.data.userAnswers[j]?app.globalData.rightA++:app.globalData.rightA;break;
      case "B": 
        this.data.rightAnswers[j]==this.data.userAnswers[j]?app.globalData.rightB++:app.globalData.rightB;break;
      case "C": 
        this.data.rightAnswers[j]==this.data.userAnswers[j]?app.globalData.rightC++:app.globalData.rightC;break;
      case "D": 
        this.data.rightAnswers[j]==this.data.userAnswers[j]?app.globalData.rightD++:app.globalData.rightD;break;
      case "E": 
        this.data.rightAnswers[j]==this.data.userAnswers[j]?app.globalData.rightE++:app.globalData.rightE;break;
    }
  }
  app.globalData.grade = app.globalData.rightA+ app.globalData.rightB+ app.globalData.rightC+ app.globalData.rightD+ app.globalData.rightE
  
  var grade = this.data.grade[29-app.globalData.ageIndex]
  var iqIndex = 0
  for(var i=1; i<9; i++){
    if(app.globalData.grade>=grade[i]){
      iqIndex = i;
      break;
    }
  }
  var scale = (this.data.standard[iqIndex-1] - this.data.standard[iqIndex]) / (grade[iqIndex-1] - grade[iqIndex]);//单位分

  var grades = (app.globalData.grade - grade[iqIndex])*scale + grade[iqIndex];
  
  //判断属于哪个等级
  switch(true){
    case grades>=95: app.globalData.iq = this.data.iq[0];break;
    case grades>=75: app.globalData.iq = this.data.iq[1];break;
    case grades>=25: app.globalData.iq = this.data.iq[2];break;
    case grades>=5: app.globalData.iq = this.data.iq[3];break;
    default:app.globalData.iq = this.data.iq[4];break;
  }
},

//计算用时
getUseTime(){
  //获取时间差
  var times = (app.globalData.endTime - app.globalData.startTime) / 1000;
  //时
  var hour = parseInt(times / 60 / 60 % 24);
  hour = hour>=10?hour:'0'+hour;
  //分
  var min = parseInt(times / 60 % 60);
  min = min>=10?min:'0'+min;
  //秒
  var s = parseInt(times % 60);
  s = s>=10?s:'0'+s;
  app.globalData.useTime = hour + ':' + min + ':' + s
  //console.log(app.globalData.useTime)
},
//将本次答题数据进行记录并提交至数据库
record(){
  let submitdate = (new Date()).format("Y-m-d");

  let data=[];
  let datetime = (new Date()).format("Y-m-d H:i");
  data.push(datetime);
  data.push(app.globalData.iq[0]);//智力等级
  data.push(app.globalData.grade);//智力分数
  data.push(app.globalData.useTime);//测试用时
  data.push(app.globalData.array[app.globalData.ageIndex].value);//年龄段

  app.globalData.times -=1;//次数减一
  //修改数据库记录 最新的时间保持在最前
  app.globalData.history.unshift(data);
  //console.log(app.globalData.history);
  
  dbuser.where({
    openid:app.globalData.openid
  }).get().then(res =>{
    dbuser.doc(res.data[0]._id).update({
      data:{
        leftTimes:app.globalData.times,
        lastDate:submitdate,
        history:app.globalData.history
      }
    })
  })
}
})
